import { ALL_CATEGORY_ITEM } from '@/constants';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import useCategoryStore from './category';

import type { Category } from '@/views/main/components/types';

const appStore = defineStore('app', () => {
  const categoryStore = useCategoryStore();

  const currentCategory = ref(ALL_CATEGORY_ITEM);
  const currentCategoryGetter = computed(() => currentCategory.value);
  const currentCategoryIndexGetter = computed(() =>
    categoryStore.categoriesGetter.findIndex(
      item => item.id === currentCategory.value.id
    )
  );

  const searchText = ref<string>('');
  const searchTextGetter = computed(() => searchText.value);

  const changeCurrentCategory = (newCategory: Category) => {
    currentCategory.value = newCategory;
  };
  const changeSearchText = (newSearchText: string) => {
    searchText.value = newSearchText;
  };

  return {
    currentCategoryGetter,
    currentCategoryIndexGetter,
    searchTextGetter,
    changeCurrentCategory,
    changeSearchText
  };
});

export default appStore;
